package com.example.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.example.AddressBook;
import com.example.domain.Person;

public class JpaAdressBook implements AddressBook {

	EntityManager em;
	
	public JpaAdressBook() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("addressbook");
		em = emf.createEntityManager();
	}
	@Override
	public void addPerson(Person person) {
		em.getTransaction().begin();
		em.persist(person);
		em.getTransaction().commit();
	}

	@Override
	public List<Person> getPersons() {
		@SuppressWarnings("unchecked")
		List<Person> persons = (List<Person>) em.createQuery("from Person").getResultList();
		return persons;
	}

}
